#!/usr/bin/env python

import gtk
import gobject
import os
import time
import gettext

gettext.install('blendIT','locale');

class signals:

   dateSelected = False;

   def __init__(self,blend):
      self.blend = blend;

   # Reload settings.xml and run main again
   def apply(self,widget):
      self.blend.gui.firstTimeWindow.destroy();
      self.blend.loadConfiguration();
      self.blend.main();

   def cancel(self,widget):
      widget.destroy();
      self.blend.kill();

   def close(self,widget):
      widget.destroy();
      self.blend.kill();

   def prep(self,widget,page):
      # What page are we on?
      if widget.get_current_page() == 2:
         window = self.blend.gui.firstTimeWindow;
         file = os.path.join(self.blend.temp,'%s.png'%window.sess);
         window.captchaImage.clear();
         window.captchaImage.set_from_file(file);
      elif widget.get_current_page() == 3:
         r = self.blend.gui.firstTimeWindow.createAccount();
         if r > 0:
            widget.set_current_page(r-1);
            return True;
         else:
            window = self.blend.gui.firstTimeWindow;
            page = window.get_nth_page(3);
            window.set_page_complete(page,True);
            return False;
         
      return False;

   # checks
   def checkDate(self,widget):
      d = widget.get_date();
      err = _("<span foreground='#FF0000'>Cannot select a future date</span>");

      window = self.blend.gui.firstTimeWindow;

      if d[0] > time.localtime().tm_year:
         # fail
         window.calInfoLabel.set_markup(err);
         self.dateSelected = False;
         return False;
      elif d[0] == time.localtime().tm_year:
         if d[1]+1 > time.localtime().tm_mon:
            # fail
            window.calInfoLabel.set_markup(err);
            self.dateSelected = False;
            return False;
         elif d[1]+1 == time.localtime().tm_mon:
            if d[2] > time.localtime().tm_mday:
               # fail
               window.calInfoLabel.set_markup(err);
               self.dateSelected = False;
               return False;

      window.calInfoLabel.set_markup('');
      self.dateSelected = True;

      self.checkEntries(widget);
         

   def checkCaptcha(self,widget):
      window = self.blend.gui.firstTimeWindow;
      page = window.get_nth_page(2);
      if len(widget.get_text()) > 0:      
         window.set_page_complete(page,True);
      else:
         window.set_page_complete(page,False);


   def checkEntries(self,widget):
      window = self.blend.gui.firstTimeWindow;

      # Check text entries
      met = True;
      # Only care if check is active
      if window.entries[0].get_active():
         if len(window.entries[2].get_text()) == 0:
            met = False;
         if self.dateSelected == False:
            met = False;
         if not window.entries[7][0].get_active() and not window.entries[7][1].get_active():
            met = False;
            

      if len(window.entries[1].get_text()) == 0:
         window.cellInfoLabel.set_markup('');
         met = False;
      else:
         try:
            d = int(window.entries[1].get_text());
            window.cellInfoLabel.set_markup('');
         except ValueError:
            err = _("<span foreground='#FF0000'>No letters allowed</span>");
            window.cellInfoLabel.set_markup(err);
            met = False;

      # Pin. Check to see if it's numbers only too     
      if len(window.entries[3].get_text()) > 0:
         try:
            d = int(window.entries[3].get_text());
            window.pinInfoLabel.set_markup(_('Pin must be more than 6 digits'));
         except ValueError:
            err = _("<span foreground='#FF0000'>Pin must only contain numbers</span>");
            window.pinInfoLabel.set_markup(err);
            met = False;
      else:
         window.pinInfoLabel.set_markup(_('Pin must be more than 6 digits'));

      if window.entries[0].get_active():
         if len(window.entries[3].get_text()) < 7:
            met = False;
            window.pinInfoLabel.set_markup(_('Pin must be more than 6 digits'));
         else:
            window.pinInfoLabel.set_markup('');
      else:
         window.pinInfoLabel.set_markup('');

      # Have the conditions been met?
      page = window.get_nth_page(1);
      window.set_page_complete(page,met);


   def checkCreateAccount(self,widget):
      entry = self.blend.gui.firstTimeWindow.entries[2];
      dob = self.blend.gui.firstTimeWindow.entries[6];
      gender = self.blend.gui.firstTimeWindow.entries[7];

      if widget.get_active():
         entry.set_sensitive(True);
         dob.set_sensitive(True);
         gender[0].set_sensitive(True);
         gender[1].set_sensitive(True);
      else:
         entry.set_sensitive(False);
         dob.set_sensitive(False);
         gender[0].set_sensitive(False);
         gender[1].set_sensitive(False);

      self.checkEntries(widget);


   def checkLang(self,widget):
      iter = widget.get_active_iter();
      model = widget.get_model();
      
      key = model.get_value(iter,1);
      if key not in ('en','af'):
         # No translations
         err = _("<span foreground='#FF0000'>No translations available (English will be used)</span>");
         self.blend.gui.firstTimeWindow.langInfoLabel.set_markup(err);
      else:
         self.blend.gui.firstTimeWindow.langInfoLabel.set_text('');